<?

require_once('./lib/authentication.lib.php');
require_once('./lib/sort.lib.php');
require_once('./lib/navigation.lib.php');

?>

<html>
<head><title>Tables</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
<SCRIPT TYPE="text/javascript" SRC="js/game.js"></SCRIPT>
<SCRIPT TYPE="text/javascript" SRC="js/sorttable.js"></SCRIPT>
<SCRIPT TYPE="text/javascript" SRC="js/tabs.js"></SCRIPT>
<STYLE>
/* Sortable tables */
table.sortable a.sortheader {
color: white;
font-weight: bold;
text-decoration: none;
}
table.sortable span.sortarrow {
color: white;
text-decoration: none;
}
</STYLE>

<?

$championship = mysql_escape_string($_GET{"id"});
$phase = mysql_escape_string($_GET["phase"]);
$homeaway = mysql_escape_string($_GET["homeaway"]);
$round_start = mysql_escape_string($_GET{"round_start"});
$round_end = mysql_escape_string($_GET{"round_end"});

?>

<STYLE TYPE="text/css">
<!--

.popupbody
{
  padding:10px;
  font-weight:bold;
  border:1px solid #CCFFFF;
  background:#99CCCC;
  color:#FFFFFF;
}

a:link
   {
   color: black;
   }
a:visited
   {
   color: black;
   }
-->
</STYLE>
</head>

<BODY>

<? echo foot_header($championship, "table"); ?>

<?
$query = "SELECT * FROM Championship WHERE id = '$championship';";
$result_champ = mysql_query($query);

$num = mysql_numrows($result_champ);

if ($num == 0)
{
  echo "<H1>Couldn't find championship $championship</H1>";
  exit;
}

$id = mysql_result($result_champ, 0, "id");
$name = mysql_result($result_champ, 0, "name");
$begin = mysql_result($result_champ, 0, "begin");
$end = mysql_result($result_champ, 0, "end");
$point_win = mysql_result($result_champ, 0, "point_win");
$point_draw = mysql_result($result_champ, 0, "point_draw");
$point_loss = mysql_result($result_champ, 0, "point_loss");

$begin = substr($begin, 0, 4);
$end = substr($end, 0, 4);
echo "<h1>$name $begin" . (($begin !== $end) ? "/$end" : "") . "</h1>\n";

if (!$phase)
{
  $query = "SELECT id FROM Phase where championship = '$id' ORDER BY `order` DESC LIMIT 1;";
  $result_phase = mysql_query($query);
  $num_phase = mysql_numrows($result_phase);
  if ($num_phase == 0)
  {
    echo "<H1>Couldn't find any phase</H1>";
    exit;
  }
  $phase = mysql_result($result_phase, 0, "id");
}

if (!$homeaway) { $homeaway = "homeaway"; }

$query = "SELECT * FROM Phase where championship = '$id' AND id = '$phase';";
$result_phase = mysql_query($query);
$num_phase = mysql_numrows($result_phase);
if ($num_phase == 0)
{
  echo "<H1>Couldn't find phase $phase</H1>";
  exit;
}

echo "<div>\n";

$query = "SELECT * FROM Phase where `championship` = $id ORDER BY `order`;";
$result_phase = mysql_query($query);
$num_phase = mysql_numrows($result_phase);

echo "<table class='tab_header'><tr>\n";
for ($i = 0; $i < $num_phase; $i++)
{
  $name_phase = mysql_result($result_phase, $i, "name");
  $id_phase = mysql_result($result_phase, $i, "id");

  if ($id_phase == $phase)
    echo "<td>$name_phase</td>\n";
  else
    echo "<td style='background-color: #999'><a href='table.php?id=$id&phase=$id_phase'>$name_phase</a></td>\n";
}
echo "</tr></table>\n";


$team_table = array();

$query = "SELECT * FROM Game WHERE phase = '$phase' ORDER BY date;";
$result_game = mysql_query($query);
$num_game = mysql_numrows($result_game);
for ($k = 0; $k < $num_game; $k++)
{
  $id_game = mysql_result($result_game, $k, "id");
  $home = mysql_result($result_game, $k, "home");
  $away = mysql_result($result_game, $k, "away");
  $phase = mysql_result($result_game, $k, "phase");
  $home_score = mysql_result($result_game, $k, "home_score");
  $away_score = mysql_result($result_game, $k, "away_score");
  $home_pen = mysql_result($result_game, $k, "home_pen");
  $away_pen = mysql_result($result_game, $k, "away_pen");
  $played = mysql_result($result_game, $k, "played");
  $date = mysql_result($result_game, $k, "date");
  $round = mysql_result($result_game, $k, "round");

  if (!$num_rounds or $round > $num_rounds) {
    $num_rounds = $round;
  }

  if ($round_start and $round_end) {
    if ($round < $round_start or $round > $round_end) {
      continue;
    }
  }

  if ($played != "played")
  {
    if (!isset($team_table[$home]["next"]))
    {
      $team_table[$home]["next"] = array($home, $away, $date);
    }
    if (!isset($team_table[$away]["next"]))
    {
      $team_table[$away]["next"] = array($home, $away, $date);
    }

    continue;
  }

  if ($homeaway != "away") {
    $team_table[$home]["last"] = array($home, $home_score, $away, $away_score, $date);
    $team_table[$home]["gf"] += $home_score;
    $team_table[$home]["ga"] += $away_score;
    $team_table[$home]["g"] += 1;
    $team_table[$home]["gp"] += $home_pen;
  }
  if ($homeaway != "home") {
    $team_table[$away]["last"] = array($home, $home_score, $away, $away_score, $date);

    $team_table[$away]["gf"] += $away_score;
    $team_table[$away]["ga"] += $home_score;
    $team_table[$away]["g"] += 1;
    $team_table[$away]["gp"] += $away_pen;
    $team_table[$away]["g_away"] += $away_score;
  }
  if ($home_score > $away_score)
  {
    if ($homeaway != "away") {
      $team_table[$home]["w"] += 1;
      $team_table[$home]["pt"] += $point_win;
    }
    if ($homeaway != "home") {
      $team_table[$away]["l"] += 1;
    }
  }
  else if ($home_score < $away_score)
  {
    if ($homeaway != "home") {
      $team_table[$away]["w"] += 1;
      $team_table[$away]["pt"] += $point_win;
    }
    if ($homeaway != "away") {
      $team_table[$home]["l"] += 1;
    }
  }
  else
  {
    if ($homeaway != "away") {
      $team_table[$home]["d"] += 1;
      $team_table[$home]["pt"] += $point_draw;
    }
    if ($homeaway != "home") {
      $team_table[$away]["d"] += 1;
      $team_table[$away]["pt"] += 1;
    }
  }
}

$query = "SELECT * FROM `Group` WHERE `phase` = $phase;";
$result_group = mysql_query($query);
$num_group = mysql_numrows($result_group);
for ($k = 0; $k < $num_group; $k++)
{
  $id_group = mysql_result($result_group, $k, "id");
  $name_group = mysql_result($result_group, $k, "name");
  $promoted = mysql_result($result_group, $k, "promoted");
  $relegated = mysql_result($result_group, $k, "relegated");

  echo "<h3>$name_group</h3>\n";

  $group_teams = array();
  $show_add = 0;

  $query = "SELECT Team.id, Team.name, Team.logo, team_group.add_sub FROM Team, team_group WHERE team_group.group = '$id_group' AND Team.id = team_group.team;";
  $result_teams = mysql_query($query);
  $num_teams = mysql_numrows($result_teams);
  $l = 0;
  while ($l < $num_teams)
  {
    $team_id = mysql_result($result_teams, $l, "Team.id");
    $team_name = mysql_result($result_teams, $l, "Team.name");
    $team_logo = mysql_result($result_teams, $l, "Team.logo");
    $add_sub = mysql_result($result_teams, $l, "team_group.add_sub");

    $group_teams[] = $team_id;

    $team_table[$team_id]["name"] = $team_name;
    $team_table[$team_id]["logo"] = $team_logo;
    if (!isset($team_table[$team_id]["g"]))
      $team_table[$team_id]["g"] = 0;
    if (!isset($team_table[$team_id]["pt"]))
      $team_table[$team_id]["pt"] = 0;
    if (!isset($team_table[$team_id]["w"]))
      $team_table[$team_id]["w"] = 0;
    if (!isset($team_table[$team_id]["d"]))
      $team_table[$team_id]["d"] = 0;
    if (!isset($team_table[$team_id]["l"]))
      $team_table[$team_id]["l"] = 0;
    if (!isset($team_table[$team_id]["gf"]))
      $team_table[$team_id]["gf"] = 0;
    if (!isset($team_table[$team_id]["ga"]))
      $team_table[$team_id]["ga"] = 0;
    if (!isset($team_table[$team_id]["gp"]))
      $team_table[$team_id]["gp"] = 0;
    if (!isset($team_table[$team_id]["g_away"]))
      $team_table[$team_id]["g_away"] = 0;

    if ($add_sub != 0) $show_add = 1;

    $team_table[$team_id]["pt"] += $add_sub;
    $team_table[$team_id]["add_sub"] = $add_sub;

    $team_table[$team_id]["gd"] = $team_table[$team_id]["gf"] - $team_table[$team_id]["ga"];
    $l++;
  }

  $team_table = multi_sort($team_table, array("pt", "w", "gd", "gf", "gp", "g_away", array("name", true)), false);

  echo "<table class='sortable indexed' id='class$id_phase'>\n";
  echo "<tr class=table_head>";
# echo "<th width=5%>Pos</th>\n";
  echo "<th width>Name</th>\n";
  echo "<th width=5%>Pts</th>\n";
  echo "<th width=5%>G</th>\n";
  echo "<th width=5%>W</th>\n";
  echo "<th width=5%>D</th>\n";
  echo "<th width=5%>L</th>\n";
  echo "<th width=5%>GF</th>\n";
  echo "<th width=5%>GA</th>\n";
  echo "<th width=5%>GD</th>\n";
  if ($show_add)
    echo "<th width=5%>Add</th>\n";
  echo "</tr>\n";
  $count = 0;
  while (list($key, $value) = each($team_table))
  {
    $ret = array_search($key, $group_teams);
    if ($ret !== false)
    {
      if ($count < $promoted)
      {
        $class = "promoted";
      }
      else if (count($team_table) - $count - 1 < $relegated)
      {
        $class = "relegated";
      }
      else
      {
        $class = "table_line";
      }
      $popup = "";
      if ($value["last"])
      {
        $popup .= conv_date($value["last"][4]) . " " . $team_table[$value["last"][0]]["name"] . " " . $value["last"][1] . " x " . $value["last"][3] . " " . $team_table[$value["last"][2]]["name"] . "<BR>";
      }
      if ($value["next"])
      {
        $popup .= conv_date($value["next"][2]) . " " . $team_table[$value["next"][0]]["name"] . " x " . $team_table[$value["next"][1]]["name"];
      }
      echo "<tr class=$class>\n";
#     echo "<td align=right>" . $count . "</td>\n";
      $count++;
      echo "<td title=\"header=[] body=[$popup] cssbody=[popupbody]\"><img border='0' width='15' height='15' src='img/logos/". preg_replace("/\.svg/", "_15.png", $value["logo"])  ."'> <a href='team.php?champ=$id&team=$key'>" . $value["name"] . "</a></td>\n";
      echo "<td align=center><b>" . $value["pt"] . "</b></td>\n";
      echo "<td align=center>" . $value["g"] . "</td>\n";
      echo "<td align=center>" . $value["w"] . "</td>\n";
      echo "<td align=center>" . $value["d"] . "</td>\n";
      echo "<td align=center>" . $value["l"] . "</td>\n";
      echo "<td align=center>" . $value["gf"] . "</td>\n";
      echo "<td align=center>" . $value["ga"] . "</td>\n";
      echo "<td align=center>" . $value["gd"] . "</td>\n";
      if ($show_add)
        echo "<td align=center>" . $value["add_sub"] . "</td>\n";
      echo "</tr>\n";
    }
  }
  echo "</table>\n";
}
?>

<FORM ACTION="table.php" METHOD="GET" NAME="round_form">

<P>Rounds:
<SELECT NAME="round_start">
<?
for ($i = 1; $i <= $num_rounds; $i++) {
if ($i == $round_start) {
echo "<OPTION value='$i' selected>$i</OPTION>";
} else {
echo "<OPTION value='$i'>$i</OPTION>";
}
}
?>
</SELECT> 
<SELECT NAME="round_end">
<?
for ($i = 1; $i <= $num_rounds; $i++) {
if ($i == $round_end or (!$round_end and $i == $num_rounds)) {
echo "<OPTION value='$i' selected>$i</OPTION>";
} else {
echo "<OPTION value='$i'>$i</OPTION>";
}
}
?>
</SELECT> 
<SELECT NAME="homeaway">
<OPTION value="homeaway"<?if ($homeaway == "homeaway") echo " selected"?>>Home and away</OPTION> 
<OPTION value="home"<?if ($homeaway == "home") echo " selected"?>>Home</OPTION> 
<OPTION value="away"<?if ($homeaway == "away") echo " selected"?>>Away</OPTION> 
</SELECT>
<INPUT TYPE="hidden" NAME="id" value="<?echo $id?>">
<INPUT TYPE="hidden" NAME="phase" value="<?echo $phase?>">
<P><INPUT TYPE="submit" NAME="submit" value="Send">

</FORM>

<?

echo "</div>\n";

echo foot_footer($championship, "table");

mysql_close();
?>


<SCRIPT TYPE="text/javascript" SRC="js/boxOver.js"></SCRIPT>
</body>
</html>

<?
function conv_date($date)
{
  $ret = substr($date, 8, 2);
  $ret .= "/";
  $ret .= substr($date, 5, 2);
  return $ret;
}
?>
